<?php
declare(strict_types=1);

namespace app\common\business;

use app\common\model\Ad as AdModel;

class Ad extends CommonBase
{
    private $_adObj = null;

    public function __construct(string $websiteCode)
    {
        $this->_websiteCode = $websiteCode;
        $this->_adObj       = new AdModel([], $websiteCode);
    }

    public function getAllAd(string $cdnDomain, array $where = [], int $limit = 0): array
    {
        $rows = [];
        try {
            $rows = $this->_adObj->getAllAd($where, $limit);
        } catch (\Exception $e) {
            logErr('读取所有广告失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        foreach ($rows as $key => $row) {
            $rows[$key]['fullImage'] = $row['image'] ? $cdnDomain . '/' . $row['image'] : '';
        }

        return $rows ? $rows->toArray() : [];
    }

    public function getAdList(string $cdnDomain, int $page = 1, int $limit = 10, array $where = []): array
    {
        $rows  = [];
        $count = 0;
        try {
            $rows  = $this->_adObj->getAdList($page, $limit, $where)->toArray();
            $count = $this->_adObj->getAdCount($where);
        } catch (\Exception $e) {
            logErr('读取广告列表失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        $positionIds = [];
        foreach ($rows as $k => $row) {
            if (!in_array($row['position_id'], $positionIds)) {
                $positionIds[] = $row['position_id'];
            }

            $rows[$k]['fullImage'] = $row['image'] ? $cdnDomain . '/' . $row['image'] : '';
        }

        $adPositionModel = new \app\common\model\AdPosition([], $this->_websiteCode);
        $adPositions     = $adPositionModel->getAllAdPosition([['id', 'in', $positionIds]])->toArray();
        $adPositionMap   = [];
        foreach ($adPositions as $row) {
            $adPositionMap[$row['id']] = $row;
        }

        foreach ($rows as $k => $row) {
            $rows[$k]['ad_position_name'] = $adPositionMap[$row['position_id']]['name'] ?? '';
        }

        return ['rows' => $rows, 'count' => $count];
    }

    public function addAd(array $data): array
    {
        try {
            return [self::_RESULT => $this->_adObj->addAd($data), self::_MESSAGE => self::_ADD_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('添加广告失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function findAdById($cdnDomain, int $id): array
    {
        $ad = null;
        try {
            $ad = $this->_adObj->findAdById($id);
        } catch (\Exception $e) {
            logErr('通过主键读取广告详细信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        if (empty($ad)) {
            return [];
        }

        $res              = $ad->toArray();
        $res['fullImage'] = $res['image'] ? $cdnDomain . '/' . $res['image'] : '';

        return $res;
    }

    public function updateAd(int $id, array $data): array
    {
        try {
            return [self::_RESULT => $this->_adObj->updateAd($id, $data), self::_MESSAGE => self::_UPDATE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键更新广告信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function deleteAd(int $id): array
    {
        try {
            return [self::_RESULT => $this->_adObj->deleteAd($id), self::_MESSAGE => self::_DELETE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键删除广告信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function batchAdSort(array $data): array
    {
        try {
            return [self::_RESULT => $this->_adObj->batchAdSort($data), self::_MESSAGE => self::_SORT_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键更新广告排序失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function batchDeleteAd(array $ids): array
    {
        try {
            return [self::_RESULT => $this->_adObj->batchDeleteAd($ids), self::_MESSAGE => self::_DELETE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键批量删除广告失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }
}
